<template>
  <div class="product-add pb50" v-loading="loading">
    <!--编辑文章-->
    <el-form size="small" :model="form" ref="form" label-width="100px">
      <div class="common-form">编辑卡券</div>
      <el-form-item label="卡券标题" prop="cardTitle">
        <el-input
          v-model="form.cardTitle"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="卡券描述"
        prop="cardContent"
        :rules="[{ required: true, message: ' ' }]"
      >
        <el-input
          v-model="form.cardContent"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="卡券售价："
        prop="sellPrice"
        :rules="[{ required: true, message: ' ' }]"
      >
        <el-input
          v-model="form.sellPrice"
          type="number"
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="市场价格："
        prop="cardPrice"
        :rules="[{ required: true, message: ' ' }]"
      >
        <el-input
          v-model="form.cardPrice"
          type="number"
          class="max-w460"
        ></el-input>
      </el-form-item>
      <!--<el-form-item label="库存：" prop="stock_num" :rules="[{ required: true, message: ' ' }]">
         <el-input v-model="form.stock_num" type="number" class="max-w460"></el-input>
       </el-form-item>-->
      <el-form-item label="卡券封面">
        <div>
          <el-button type="primary" @click="openUpload">上传图片</el-button>
          <img class="mt10" v-img-url="form.filePath" :width="120" alt="" />
          <!--上传图片组件-->
          <Upload
            v-if="isupload"
            :isupload="isupload"
            @returnImgs="returnImgsFunc"
            >上传图片</Upload
          >
        </div>
      </el-form-item>
      <el-form-item label="卡券分类">
        <el-select
          v-model="form.categoryId"
          placeholder="请选择"
          style="width: 460px"
        >
          <el-option
            v-for="(item, index) in category"
            :key="index"
            :label="item.name"
            :value="item.categoryId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联商品">
        <el-select
          v-model="form.productId"
          filterable
          placeholder="请选择"
          style="width: 460px"
        >
          <el-option
            v-for="(item, index) in product"
            :key="index"
            :label="item.productName"
            :value="item.productId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品规格" prop="productAttr">
        <el-input
          v-model="form.productAttr"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item label="卡券状态">
        <el-radio-group v-model="form.cardStatus">
          <el-radio :label="10">上架</el-radio>
          <el-radio :label="20">下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序">
        <el-input
          type="number"
          v-model="form.cardSort"
          class="max-w460"
        ></el-input>
        <div class="tips">数字小的排前面</div>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button
          size="small"
          type="info"
          @click="cancelFunc"
          :loading="loading"
          >取消</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="onSubmit"
          :loading="loading"
          >提交</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import CardApi from "@/api/card.js";
import Upload from "@/components/file/Upload.vue";
export default {
  components: {
    /*图片上传*/
    Upload,
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*是否上传图片*/
      isupload: false,
      /*form表单数据*/
      form: {
        cardId: 0,
        cardTitle: "",
        categoryId: "",
        imageId: "",
        cardSort: 100,
        cardStatus: 10,
        cardContent: "",
        stockNum: "",
        productId: "",
        cardPrice: "",
        sellPrice: "",
        productAttr: "",
      },
      /*新闻类别*/
      category: [],
      product: [],
    };
  },
  created() {
    this.getDetail();
  },

  methods: {
    /*上传*/
    openUpload() {
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      let self = this;
      if (e != null) {
        this.form.imageId = e[0].fileId;
        this.form.filePath = e[0].filePath;
      }
      this.isupload = false;
    },

    getDetail() {
      let self = this;
      // 取到路由带过来的参数
      const params = self.$route.query.cardId;
      CardApi.toEditCard({ cardId: params }, true)
        .then((res) => {
          self.form = res.data.card;
          if (!self.form.image) {
            self.form.image = {};
          }
          self.category = res.data.category;
          self.product = res.data.product;
          self.loading = false;
        })
        .catch((error) => {});
    },

    /*修改文章*/
    onSubmit() {
      let self = this;
      let params = self.form;
      params.cardId = self.$route.query.cardId;
      // 取到路由带过来的参数
      CardApi.editCard(params, true)
        .then((data) => {
          ElMessage({
            message: data.msg,
            type: "success",
          });
          self.cancelFunc();
        })
        .catch((error) => {});
    },

    /*取消添加，返回文章列表*/
    cancelFunc() {
      this.$router.back(-1);
    },
  },
};
</script>

<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>
